package com.googlecode.hypersphere.ejbJarXmlTransformer;

import java.io.File;
import java.util.Iterator;

import junit.framework.TestCase;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLMock {

	public boolean isIdentical(File file1, File file2) throws DocumentException {
		SAXReader xmlReader = new SAXReader();
		Document doc1 = xmlReader.read(file1);
		Document doc2 = xmlReader.read(file2);
		Element element1 = doc1.getRootElement();
		Element element2 = doc2.getRootElement();
		test(element1, element2);
		return true;
	}
	
	private void test(Element element1, Element element2) {
		testAttributes(element1, element2);
		Iterator iterator1 = element1.elementIterator();
		Iterator iterator2 = element2.elementIterator();
		TestCase.assertEquals(element1.getName() + " - " + element2.getName(),
				element1.getName(), element2.getName());
		TestCase.assertEquals(element1.getName() + " - " + element2.getName(),
				iterator1.hasNext(), iterator2.hasNext());
		while (iterator1.hasNext()) {
			element1 = (Element) iterator1.next();
			element2 = (Element) iterator2.next();
			test(element1, element2);
		}
	}

	public boolean testAttributes(Element e1, Element e2) {
		Iterator iterator1 = e1.attributeIterator();
		Iterator iterator2 = e2.attributeIterator();
		TestCase.assertEquals(iterator1.hasNext(), iterator2.hasNext());
		while (iterator1.hasNext()) {
			Attribute attribute1 = (Attribute) iterator1.next();
			Attribute attribute2 = (Attribute) iterator2.next();
			TestCase.assertEquals(attribute1.getName(), attribute2.getName());
			TestCase.assertEquals(attribute1.getValue(), attribute2.getValue());
		}
		return true;
	}
}
